package com.hzhk.happyrent.ui.activity.order;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hzhk.happyrent.Constants;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.CodeModel;
import com.hzhk.happyrent.bean.order.OrderBuyPayModel;
import com.hzhk.happyrent.bean.order.OrderReletBean;
import com.hzhk.happyrent.network.API;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.activity.PayPeriodsActivity;
import com.hzhk.happyrent.ui.adapter.ProductByStagesAdapter;
import com.hzhk.happyrent.ui.view.PredicateLayout;
import com.hzhk.happyrent.utils.ListUtils;
import com.hzhk.happyrent.utils.MyPerference;
import com.hzhk.happyrent.utils.glide.ShowImageUtils;
import com.tamic.novate.Throwable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderReletActivity extends Activity {

    @BindView(R.id.activity_titlebar_center)
    TextView activityTitlebarCenter;
    @BindView(R.id.pre_otder_phone_image)
    ImageView preOtderPhoneImage;
    @BindView(R.id.pre_otder_phone_name)
    TextView preOtderPhoneName;
    @BindView(R.id.layout_product_type)
    PredicateLayout layoutProductType;
    @BindView(R.id.pre_price)
    TextView prePrice;
    @BindView(R.id.buy_pay_day)
    TextView buyPayDay;
    @BindView(R.id.relet_choose)
    TextView reletChoose;

    private PopupWindow mPopupWindow;
    private OrderReletBean orderReletBean;
    private List<String> mList;
    String money;
    String rent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_relet);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initData(OrderBuyPayModel orderBuyPayModel) {
//        intent = getIntent();
//        //addressPrePhone.setText(intent.getStringExtra(Constants.MOBILE));
//        //addressPreName.setText(intent.getStringExtra(Constants.CONSIGNEE));
//        prePrice.setText(intent.getStringExtra(Constants.TOTAL_PRICE)+"元");
//        preOtderPhoneName.setText(intent.getStringExtra(Constants.GOODS_NAME));
//        buyPayDate.setText(orderBuyPayModel.getData().getBuy_info().getDay()+"天");
//        buyPayPrice.setText("¥"+orderBuyPayModel.getData().getBuy_info().getPrice_paid());
//        buyPayTotalMoney.setText("¥"+orderBuyPayModel.getData().getBuy_info().getPay_money());
//        ShowImageUtils.showImageViewToCircleNoPlaceHolder(App.getContext(), intent.getStringExtra(Constants.GOODS_IMAGE), preOtderPhoneImage, 0);
//        String attr_key_path = intent.getStringExtra(Constants.ATTR_KEY_PATH);
//        if (!TextUtils.isEmpty(attr_key_path)){
//            predicateLayout.removeAllViews();
//            String description = attr_key_path.substring(1, attr_key_path.length() - 1);
//            if (description.contains("][")) {
//                String[] tag = description.split("]\\u005B");
//                for (String s : tag) {
//                    TextView textView = new TextView(this);
//                    textView.setText(s);
//                    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
//                    textView.setPadding(5, 0, 5, 0);
//                    textView.setTextColor(this.getResources().getColor(R.color.tab_red_80));
//                    //textView.setBackgroundResource(R.drawable.bg_red_hollow);
//                    textView.setBackground(this.getResources().getDrawable(R.drawable.bg_red_hollow));
//                    predicateLayout.addView(textView);
//                }
//            } else {
//                TextView textView = new TextView(this);
//                textView.setText(description);
//                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
//                textView.setPadding(5, 0, 5, 0);
//                textView.setTextColor(this.getResources().getColor(R.color.tab_red_80));
//                //textView.setBackgroundResource(R.drawable.bg_red_hollow);
//                textView.setBackground(this.getResources().getDrawable(R.drawable.bg_red_hollow));
//                predicateLayout.addView(textView);
//            }
//        }
        //preOtderPhoneName.setText(intent.getStringExtra(Constants.CONSIGNEE));
    }

    private void initView() {
        activityTitlebarCenter.setText("续租");
        ShowImageUtils.showImageView(this,preOtderPhoneImage,getIntent().getStringExtra("img_product"));
        preOtderPhoneName.setText(getIntent().getStringExtra("product_title"));
        prePrice.setText(getIntent().getStringExtra("product_value"));
        if (!TextUtils.isEmpty(getIntent().getStringExtra("product_description"))) {
            layoutProductType.removeAllViews();
            String description = getIntent().getStringExtra("product_description").substring(1, getIntent().getStringExtra("product_description").length() - 1);
            if (description.contains("][")) {
                String[] tag = description.split("]\\u005B");
                for (String s : tag) {
                    TextView textView = new TextView(this);
                    textView.setText(s);
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                    textView.setPadding(5, 0, 5, 0);
                    textView.setTextColor(getResources().getColor(R.color.tab_red_80));
                    //textView.setBackgroundResource(R.drawable.bg_red_hollow);
                    textView.setBackground(getResources().getDrawable(R.drawable.bg_red_hollow));
                    layoutProductType.addView(textView);
                }
            } else {
                TextView textView = new TextView(this);
                textView.setText(description);
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                textView.setPadding(5, 0, 5, 0);
                textView.setTextColor(getResources().getColor(R.color.tab_red_80));
                //textView.setBackgroundResource(R.drawable.bg_red_hollow);
                textView.setBackground(getResources().getDrawable(R.drawable.bg_red_hollow));
                layoutProductType.addView(textView);
            }
        }
    }

    @OnClick({R.id.activity_titlebar_left, R.id.act_login_submit, R.id.relet_choose})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_titlebar_left:
                finish();
                break;
            case R.id.act_login_submit:
                Intent intent = new Intent(this, PayPeriodsActivity.class);
                intent.putExtra("order_id", getIntent().getStringExtra("order_id"));
                intent.putExtra("zu_date", rent);
                intent.putExtra("pay_total", money);
                intent.putExtra("come_from", "OrderReletActivity");
                startActivity(intent);
                break;
            case R.id.relet_choose:
                if(!ListUtils.isEmpty(mList)){
                    show(reletChoose);
                }
                break;
        }
    }
    private void initData(){
        Map<String, Object> params = new HashMap<>();
        params.put(Constants.ORDER_ID, getIntent().getStringExtra("order_id"));
        HttpManager.getInstance().get(API.GET_ORDER_GO_ON_DATA, params, new MyStringResponse(this) {
            @Override
            public void onCompleted(Object tag){
                super.onCompleted(tag);
            }

            @Override
            public void onError(Object tag, Throwable e) {

            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }

            @Override
            public void onNext(Object tag, String response) {
                orderReletBean = new Gson().fromJson(response, OrderReletBean.class);
                if(orderReletBean.getErrCode() == 0){
                    buyPayDay.setText("￥"+orderReletBean.getData().get(0).getMoney()+"/"+orderReletBean.getData().get(0).getZuDate()+"个月");
                    reletChoose.setText(orderReletBean.getData().get(0).getZuDate()+"个月");
                    money=orderReletBean.getData().get(0).getMoney();
                    rent=orderReletBean.getData().get(0).getZuDate();
                    if(!ListUtils.isEmpty(orderReletBean.getData())){
                        mList = new ArrayList<>();
                        for (OrderReletBean.DataBean dataBean : orderReletBean.getData()) {
                            mList.add(dataBean.getZuDate()+"个月");
                        }
                    }
                }

            }
        });
    }
    public void show(View v) {
        try {
            if (mPopupWindow == null) {

                View mView = LayoutInflater.from(this).inflate(R.layout.menu_select_city, null);
                final ListView city_lv = (ListView) mView.findViewById(R.id.city_lv);
                city_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> av, View v,
                                            int position, long id) {
                        buyPayDay.setText("￥"+orderReletBean.getData().get(position).getMoney()+"/"+((ProductByStagesAdapter) (av.getAdapter())).getItem(position));
                        reletChoose.setText(orderReletBean.getData().get(position).getZuDate()+"个月");
                        money=orderReletBean.getData().get(position).getMoney();
                        rent=orderReletBean.getData().get(position).getZuDate();
                        hide();
                    }
                });
                ProductByStagesAdapter adapter_test = new ProductByStagesAdapter(this);
                if (adapter_test != null) {
                    adapter_test.setList(mList);
                }
                city_lv.setAdapter(adapter_test);
                mPopupWindow = new PopupWindow(mView,
                        LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,
                        true);
                // mPopupWindow.setAnimationStyle(R.style.popwin_more_anim_style);
                mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
                mPopupWindow.setTouchable(true);
                mPopupWindow.setFocusable(true);
                mPopupWindow.setSplitTouchEnabled(true);
                mPopupWindow.setOutsideTouchable(true);
                mPopupWindow.setTouchInterceptor(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                            hide();
                            return true;
                        }
                        return false;
                    }
                });
            }

            mPopupWindow.showAsDropDown(v);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void hide() {
        if (mPopupWindow != null) {
            mPopupWindow.dismiss();
            mPopupWindow = null;
        }
        return;
    }
}
